package com.bainaeco.framework.enums;

import java.util.Locale;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.commons.lang3.StringUtils;

import com.bainaeco.framework.exception.InvalidArgumentException;

public enum Lang {
	EN_US("en_US"), // en_US
	ZH_CN("zh_CN"), // zh_CN	
	ZH_HK("zh_HK"), // zh_HK
	;

	public Map<String, Locale> localeMap = new ConcurrentHashMap<String, Locale>();

	private String lang;

	private Lang(String lang) {
		this.lang = lang;
	}

	public String toStringValue() {
		return this.lang;
	}

	public Locale toLocale() {
		Locale locale = localeMap.get(this.lang);
		if (locale == null) {
			String[] langCodes = this.lang.split("_");
			locale = new Locale(langCodes[0], langCodes[1]);
			localeMap.put(this.lang, locale);
		}

		return locale;
	}

	public static Lang toLang(String lang) {
		if (StringUtils.equals(EN_US.lang, lang)) {
			return EN_US;
		} else if (StringUtils.equals(ZH_CN.lang, lang)) {
			return ZH_CN;
		} else if (StringUtils.equals(ZH_HK.lang, lang)) {
			return ZH_HK;
		} else {
			throw new InvalidArgumentException("未知类型[" + lang + "].");
		}
	}

	public static Lang getDefaultLang(){
		return ZH_CN;
	}
}
